package com.work.saturn.mine;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.immo.libcommon.base.BaseFragment;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.constant.SharePreferenceKey;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.SharePreferenceUtils;
import com.work.saturn.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;

import com.immo.libcommon.view.MyDialog;
import com.work.saturn.bean.UserMsgBean;
import com.immo.libcommon.constant.RouterUtil;


import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * @author Administrator
 * @content 我的
 * @date 2018/7/10
 */
@Route(path = RouterUtil.MINE_FRAGMENT)
public class MineFragment extends BaseFragment {
    Unbinder unbinder;
    @BindView(R.id.user_id)
    TextView mUserId;
    private boolean isPrepared = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        isPrepared = true;
        lazyLoad();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        initUI();
    }

    private void initUI() {
        mUserId.setText(Objects.requireNonNull(SharePreferenceUtils.get(Objects.requireNonNull(getActivity()), SharePreferenceKey.USERNAME, "")).toString());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 退出登录
     */
    private void toOutLogin() {
        SharePreferenceUtils.put(Objects.requireNonNull(getActivity()), SharePreferenceKey.USERNAME, "");
        SharePreferenceUtils.put(getActivity(), SharePreferenceKey.TOKEN, "");
        ARouter.getInstance().build(RouterUtil.LOGIN_PAGE).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation();
        getActivity().finish();
    }

    /**
     * 获取用户信息
     */
    private void getUserMsg() {

        new NetworkRequest((object, data) -> {
            if (object instanceof UserMsgBean) {
                UserMsgBean userMsgBean = (UserMsgBean) object;
                if (userMsgBean.isSuccess()) {
                    if (userMsgBean.getPayload().isCanCreateNewToken()) {
                        ARouter.getInstance().build(RouterUtil.CREATE_TOKEN).navigation();
                    } else {
                        toast("您的账号没有创建Token权限，请联系管理员授予");
                    }
                }

            }
        }).requestGet(BaseUrl.GETUSERMSG + Objects.requireNonNull(SharePreferenceUtils.get(Objects.requireNonNull(getActivity()), SharePreferenceKey.USERNAME, "")).toString(),
                getActivity(), UserMsgBean.class, true, false);
    }

    @OnClick({R.id.user_token, R.id.user_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_token:
                getUserMsg();
                break;
            case R.id.user_loginout:
                MyDialog.dialogShow(getActivity(), "你确定要退出账号吗？", "取消", "确定", this::toOutLogin);
                break;
        }
    }
}
